package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.Dynamic;
import com.example.entity.res.DynamicRes;
import com.example.mapper.DynamicMapper;
import com.example.service.DynamicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
@Slf4j
public class DynamicServiceImpl implements DynamicService {

    @Autowired
    DynamicMapper dynamicMapper;

    @Override
    public List<DynamicRes> selectAll() {
        List<Dynamic> dynamics = dynamicMapper.selectAll();
        List<DynamicRes> dynamicResLists = new ArrayList<>();
        for (Dynamic dynamic : dynamics) {
            DynamicRes dynamicRes = new DynamicRes();
            if (!(dynamic.getTitle() == null)) {
                DynamicRes child = this.getChild(Integer.valueOf(dynamic.getTitle()), dynamic.getUid(), dynamic.getCreatDate());
                dynamicResLists.add(child);
            } else {
                List<Integer> list = new ArrayList<>();
                if (dynamic.getImgId().equals("")) {
                    list = null;
                } else {
                    String[] splits = dynamic.getImgId().split(",");
                    for (String split : splits) {
                        list.add(Integer.valueOf(split));
                    }
                }
                dynamicRes.setId(dynamic.getId());
                dynamicRes.setTitle(null);
                dynamicRes.setDynamic(dynamic.getDynamic());
                dynamicRes.setCreatDate(dynamic.getCreatDate());
                dynamicRes.setUid(dynamic.getUid());
                dynamicRes.setImgId(list);
                dynamicRes.setIsShare(false);
                dynamicResLists.add(dynamicRes);
            }
        }
        return dynamicResLists;
    }

    @Override
    public List<DynamicRes> getDynamicHimself(Integer uid) {
        List<Dynamic> dynamics = dynamicMapper.getDynamicHimself(uid);
        List<DynamicRes> dynamicResLists = new ArrayList<>();
        for (Dynamic dynamic : dynamics) {
            DynamicRes dynamicRes = new DynamicRes();
            if (!(dynamic.getTitle() == null)) {
                DynamicRes child = this.getChild(Integer.valueOf(dynamic.getTitle()), dynamic.getUid(), dynamic.getCreatDate());
                dynamicResLists.add(child);
            } else {
                List<Integer> list = new ArrayList<>();
                if (dynamic.getImgId().equals("")) {
                    list = null;
                } else {
                    String[] splits = dynamic.getImgId().split(",");
                    for (String split : splits) {
                        list.add(Integer.valueOf(split));
                    }
                }
                dynamicRes.setId(dynamic.getId());
                dynamicRes.setTitle(null);
                dynamicRes.setDynamic(dynamic.getDynamic());
                dynamicRes.setCreatDate(dynamic.getCreatDate());
                dynamicRes.setUid(dynamic.getUid());
                dynamicRes.setImgId(list);
                dynamicRes.setIsShare(false);
                dynamicResLists.add(dynamicRes);
            }
        }
        return dynamicResLists;
    }

    @Override
    public List<DynamicRes> keywordsSearch(String keywords) {
        List<Dynamic> dynamics = dynamicMapper.selectList(new QueryWrapper<Dynamic>().like("dynamic", keywords));


        List<DynamicRes> dynamicResLists = new ArrayList<>();
        for (Dynamic dynamic : dynamics) {
            DynamicRes dynamicRes = new DynamicRes();
            if (!(dynamic.getTitle() == null)) {
                DynamicRes child = this.getChild(Integer.valueOf(dynamic.getTitle()), dynamic.getUid(), dynamic.getCreatDate());
                dynamicResLists.add(child);
            } else {
                List<Integer> list = new ArrayList<>();
                if (dynamic.getImgId().equals("")) {
                    list = null;
                } else {
                    String[] splits = dynamic.getImgId().split(",");
                    for (String split : splits) {
                        list.add(Integer.valueOf(split));
                    }
                }
                dynamicRes.setId(dynamic.getId());
                dynamicRes.setTitle(null);
                dynamicRes.setDynamic(dynamic.getDynamic());
                dynamicRes.setCreatDate(dynamic.getCreatDate());
                dynamicRes.setUid(dynamic.getUid());
                dynamicRes.setImgId(list);
                dynamicRes.setIsShare(false);
                dynamicResLists.add(dynamicRes);
            }
        }
        return dynamicResLists;
    }

    public DynamicRes getChild(Integer id, Integer uid, Date date) {
        Dynamic dynamic = dynamicMapper.selectByPrimaryKey(id);
        DynamicRes dynamicRes = new DynamicRes();
        List<Integer> list = new ArrayList<>();
        if (dynamic == null) {
            dynamicRes.setIsHaveFather(false);
            dynamicRes.setId(id);
            dynamicRes.setTitle(null);
            dynamicRes.setDynamic(null);
            dynamicRes.setCreatDate(null);
            dynamicRes.setUid(null);
            dynamicRes.setImgId(null);
            dynamicRes.setIsShare(null);
            dynamicRes.setShareManId(uid);
            dynamicRes.setShareManDate(null);
            return dynamicRes;
        } else {
            if (dynamic.getImgId().equals("")) {
                list = null;
            } else {
                String[] splits = dynamic.getImgId().split(",");
                for (String split : splits) {
                    list.add(Integer.valueOf(split));
                }
            }
            dynamicRes.setId(dynamic.getId());
            dynamicRes.setTitle(null);
            dynamicRes.setDynamic(dynamic.getDynamic());
            dynamicRes.setCreatDate(dynamic.getCreatDate());
            dynamicRes.setUid(dynamic.getUid());
            dynamicRes.setImgId(list);
            dynamicRes.setIsShare(true);
            dynamicRes.setShareManId(uid);
            dynamicRes.setShareManDate(date);
            return dynamicRes;
        }
    }

    @Override
    public void insert(Dynamic record) {
        dynamicMapper.insert(record);
    }

    @Override
    public void deleteByPrimaryKey(Integer id) {
        dynamicMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Dynamic selectByPrimaryKey(Integer id) {
        return dynamicMapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteByTitle(String title) {
        dynamicMapper.deleteByTitle(title);
    }

    @Override
    public List<DynamicRes> listAllDynamic() {
        List<Dynamic> dynamics = dynamicMapper.selectAll();
        List<DynamicRes> dynamicResLists = new ArrayList<>();
        for (Dynamic dynamic : dynamics) {
            DynamicRes dynamicRes = new DynamicRes();
            if (!(dynamic.getTitle() == null)) {
                DynamicRes child = this.getChild(Integer.valueOf(dynamic.getTitle()), dynamic.getUid(), dynamic.getCreatDate());
                dynamicResLists.add(child);
            } else {
                List<Integer> list = new ArrayList<>();
                if (dynamic.getImgId().equals("")) {
                    list = null;
                } else {
                    String[] splits = dynamic.getImgId().split(",");
                    for (String split : splits) {
                        list.add(Integer.valueOf(split));
                    }
                }
                dynamicRes.setId(dynamic.getId());
                dynamicRes.setTitle(null);
                dynamicRes.setDynamic(dynamic.getDynamic());
                dynamicRes.setCreatDate(dynamic.getCreatDate());
                dynamicRes.setUid(dynamic.getUid());
                dynamicRes.setImgId(list);
                dynamicRes.setIsShare(false);
                dynamicResLists.add(dynamicRes);
            }
        }
        return dynamicResLists;
    }
}
